package org.swidgets.base.spanels;

import java.util.NoSuchElementException;

import org.swidgets.base.BorderStyle;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;


/**
 * 
 * Internal Class
 *
 */
public class SlimPanel extends Composite{
	private final String  id;
	private final HTMLPanel  content;

	public static final String generateHtmlBorderTable(BorderStyle style, String classStyleOver, String clasStyleClicked, String id){
		StringBuffer htmlBorderTable = new StringBuffer();
		
		htmlBorderTable.append("<table cellspacing='0' cellpadding='0' border='0px' width='100%' height='100%' class='" + style.getMain() + "' style='font-size:0px'>");
		
		//prima riga
		htmlBorderTable.append("<tr>");
		
		htmlBorderTable.append("<td>");
		htmlBorderTable.append("<img src='" + style.getUrlImgTopLeft() + "'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("<td class='" + style.getTop() + "'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("<td>");
		htmlBorderTable.append("<img src='" + style.getUrlImgTopRight() + "'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("</tr>");


		//seconda riga
		htmlBorderTable.append("<tr>");
		
		htmlBorderTable.append("<td class='" + style.getLeft() + "'>");;
		htmlBorderTable.append("</td>");
		
		//cella contenente il widget
		
		if (classStyleOver!= "" || clasStyleClicked != ""){
			htmlBorderTable.append("<td id='" + id + "' class='" + style.getContent() +"' ");
			htmlBorderTable.append("onMouseOver=\"this.className ='" + classStyleOver +"'\" ");
			htmlBorderTable.append("onMouseDown=\"this.className ='" + clasStyleClicked +"'\" ");
			htmlBorderTable.append("onMouseUp=\"this.className ='" + classStyleOver +"'\" ");
			htmlBorderTable.append("onMouseOut=\"this.className ='" + style.getContent() +"'\" ");
			htmlBorderTable.append("></td>");					
		}else{
			htmlBorderTable.append("<td id='" + id + "' class='" + style.getContent() +"'></td>");	
		}
		
		htmlBorderTable.append("<td class='" + style.getRight() + "'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("</tr>");


		//terza riga
		htmlBorderTable.append("<tr>");
		
		htmlBorderTable.append("<td>");
		htmlBorderTable.append("<img src='" + style.getUrlImgBottomLeft() + "'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("<td class='" + style.getBottom() +"'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("<td>");
		htmlBorderTable.append("<img src='" + style.getUrlImgBottomRight() + "'>");
		htmlBorderTable.append("</td>");
		
		htmlBorderTable.append("</tr>");
		
		htmlBorderTable.append("</table>");
		
		return htmlBorderTable.toString();
	}
	

	public SlimPanel(BorderStyle style){
		this(style, "", "");
	}  
	
	public SlimPanel(BorderStyle style, String classStyleOver, String clasStyleClicked){
		super();
		
		id = HTMLPanel.createUniqueId();
		content = new HTMLPanel(generateHtmlBorderTable(style, classStyleOver, clasStyleClicked, id));

		initWidget(content);
		content.setHeight("0px");
		content.setWidth("0px");
	}  

	public void add(Widget w){
		content.add(w, id);
	}
	
	public void setHtml(String html){
	    Element elem = getElementById(content.getElement(), id);
	    if (elem == null) {
	      throw new NoSuchElementException(id);
	    }
	    
	    DOM.setInnerHTML(elem, html);
	}
	
	/*
	   * Implements getElementById() downward from the given element. We need to do
	   * this because {@link #add(Widget, String)} must often be called before the
	   * panel is attached to the DOM, so {@link Dom#getElementById} won't yet work.
	   */
	  private Element getElementById(Element elem, String id) {
	    String elemId = DOM.getElementProperty(elem, "id");
	    if ((elemId != null) && elemId.equals(id)) {
	      return elem;
	    }

	    Element child = DOM.getFirstChild(elem);
	    while (child != null) {
	      Element ret = getElementById(child, id);
	      if (ret != null) {
	        return ret;
	      }
	      child = DOM.getNextSibling(child);
	    }

	    return null;
	  }
}
